package com.liang.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //放行的两种情况：
        //1、session不为空
        //2、login相关的页面：表示第一次登录（即此时还没有session信息，但不能够被拦截），或是进入登录页面
        var session = request.getSession();
        if(session.getAttribute("userLoginInfo")!=null){
            return true;
        }
        if(request.getRequestURI().contains("goLogin") || request.getRequestURI().contains("login")){
            return true;
        }

        request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request,response);
        return false;
    }
}
